import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
 
/**
 * 首页
 */

@Component({
    templateUrl: './equipment.html',
    styleUrls: ['./equipment.scss'],
})
export class EquipmentComponent implements OnInit {
    pages={
        pageNo:1,
        pageSize:10,
		keywords:"", // 名称模糊查询
    	isEnable:"" // 已开启 Y是,N否, 或空
    };
    dataList = [];
    count= {};
    tableLoading = false;
    ngOnInit() {
        this.loadList();
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
    changeFilter(){
        this.pages.pageNo = 1;
        this.loadList();
    }
    loadList(){
        this.apiService.post('接口设备列表',this.pages).then((res:any)=>{
            this.dataList = res.data.list;
        })
    }
    openEquipment(data){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认开启当前设备？',
			nzOkText: '确认',
			nzOkType: 'primary',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('开启/关闭接入设备', { id: data.id, enable: 'Y' })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('设备开启成功！');
							this.loadList();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
    closeEquipment(data){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认关闭当前设备？',
			nzOkText: '确认',
			nzOkType: 'primary',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('开启/关闭接入设备', { id: data.id, isEnable: 'N' })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('设备关闭成功！');
							this.loadList();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
    deleteEquipment(data){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认删除当前设备？',
			nzOkText: '删除',
			nzOkType: 'danger',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('删除接入设备', { id: data.id })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('设备删除成功！');
							this.loadList();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
}
